package com.tywj.lib.core.common.util

import android.app.Activity
import android.content.Context
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import java.lang.reflect.Method
import java.util.regex.Pattern

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/7
 *     desc   : 输入工具类
 *     version: 1.0.0
 * </pre>
 */
object InputUtil {

    /**
     * 将输入数据时实转换为大写
     */
    fun toUpperCase(et: EditText) {
        et.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable?) {}

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                //如果车牌中含有小写字母，则使用大写字母代替
                val defStr = s.toString()
                val upStr = defStr.toUpperCase()
                if (defStr != upStr) {
                    val startIndex = et.selectionStart
                    et.setText(upStr)
                    et.setSelection(startIndex)
                }
            }

        })
    }

    /**
     * 不显示系统键盘,显示光标
     */
    fun notShowSoftInput(activity: Activity, ed: EditText) {
        activity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
        val currentVersion = android.os.Build.VERSION.SDK_INT
        var methodName: String? = null
        if (currentVersion >= 16) {
            // 4.2
            methodName = "setShowSoftInputOnFocus"
        } else if (currentVersion >= 14) {
            // 4.0
            methodName = "setSoftInputShownOnFocus"
        }

        if (methodName == null) {
            ed.inputType = InputType.TYPE_NULL
        } else {
            val cls = EditText::class.java
            val setShowSoftInputOnFocus: Method
            try {
                setShowSoftInputOnFocus = cls.getMethod(methodName, Boolean::class.javaPrimitiveType)
                setShowSoftInputOnFocus.isAccessible = true
                setShowSoftInputOnFocus.invoke(ed, false)
            } catch (e: Exception) {
                ed.inputType = InputType.TYPE_NULL
                LogUtil.printStackTrace(e)
            }
        }
    }

    /**
     * 显示输入法键盘
     */
    fun showSoftInput(activity: Activity, view: EditText) {
        view.isFocusable = true
        view.isFocusableInTouchMode = true
        view.requestFocus()
        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }

    /**
     * 隐藏输入法键盘
     */
    fun hideSoftInput(activity: Activity, view: EditText) {
        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }

    /**
     * 编辑框过虑掉特殊字符
     */
    fun stringFilter(str: String): String {
        val p = Pattern.compile(RegexUtil.REGEX_STRING)
        val m = p.matcher(str)
        return m.replaceAll("").trim()
    }
}